/**
 * 产品修改js
 */
var comValidform = null;
var returnStatus = false;
var productPayBack = {
	key : null,
	dataCache : null,
	startDiv : null,
	payBackTime: null,
	time:10,
	itv:null,
	prodId:null,
	canPayBack:null,
	errorMsg:null,
	code01:null,
	code02:null,
    creditorType:0, //债权人性质，0：个人，1：企业
    repayPoundage:null,//手续费

	showProdOrderInfo:function(){


		var prodId=art.dialog.data('proId');  //修改id

		productPayBack.key = prodId+"_ProductType02_Key";


		var ret = eval('(' + localStorage.getItem(productPayBack.key) + ')');

		/*根据债权人性质来控制回款手续费*/
        productPayBack.creditorType = ret.creditorType;
        if(productPayBack.creditorType ==1){
            $(".repayPoundage").show();
		}

		$('#com-addForm td[name=prodName]').html(ret.name);


		var time = ret.proDividendDate ==null?'':(new Date(ret.proDividendDate)).format('yyyy-MM-dd');


		$('#com-addForm td[name=orderPayBackTime]').html(time);

		productPayBack.prodId = prodId;
		var url = 'mgr/ProductInfoCtrl/getProdOrderInfo';



		var params ={
			'id':prodId
		};

		var cb = function(data){
			if(data.resultCode==200){
				var ret=data.result;
				for(var i in ret){
					var text=ret[i];

					if(i=='orderPayBackTime'){
						productPayBack.payBackTime = text;
						text=text==null?'':(new Date(text)).format('yyyy-MM-dd');
					}

					$('#com-addForm td[name="'+i+'"]').html(text);



				}
				$("input[name='code01']").val("短信验证码正在发送");
				$("input[name='code02']").val("短信验证码正在发送");


				productPayBack.getPayBackPhoneCode();



				productPayBack.itv = setInterval(function(){

					document.getElementById("msg").innerHTML =  "<h2>请在</h2>";

					document.getElementById("timer").innerHTML =  "<h2>" + productPayBack.time + "秒后进行回款操作</h2>";

					productPayBack.time = productPayBack.time - 1;

					if(productPayBack.time < 0){
						clearInterval(productPayBack.itv);
						$("#productType02_107").attr("disabled",false);
					}else{
						$("#productType02_107").attr("disabled",true);
					}

				}, 1000);

			}else{
				alert(data.msg);
				document.getElementById("msg").innerHTML =  "<h2>错误信息是：</h2>";
				document.getElementById("timer").innerHTML =  "<h2>" + data.msg + "</h2>";
				$("#productType02_107").attr("disabled",true);
			}
		};

		sendApiNoLoadSync(url, params, cb, 'POST');
	},
	startPayBack : function() {
		var id = productPayBack.prodId;

		var url = 'mgr/ProductInfoCtrl/httPayback';


		var params = {
			'productId' : id,
			'userId' : userId,
			'code01' : productPayBack.code01,
			'code02' : productPayBack.code02,
			'repayPoundage':productPayBack.repayPoundage
		};


		var cb = function (data) {
			alert(data.msg);
			if (data.resultCode != 200) {
				document.getElementById("msg").innerHTML = "<h2>错误信息是：</h2>";
				document.getElementById("timer").innerHTML = "<h2>" + data.msg + "</h2>";
				$("#productType02_107").attr("disabled", true);
			}
		};

		sendApiNoLoad(url, params, cb, 'POST');
	},
	getPayBackPhoneCode : function() {
		var id = productPayBack.prodId;

		var url = 'mgr/ProductInfoCtrl/getPayBackCode';


		var params = {
			'productId' : id,
			'userId' : userId
		};
		var cb = function (data) {
				$("input[name='code01']").val("请输入短信验证码");
				$("input[name='code02']").val("请输入短信验证码");
			//if (data.resultCode == 200) {
			//	$("input[name='code01']").val("请输入短信验证码");
			//	$("input[name='code02']").val("请输入短信验证码");
			//}else{
            //
            //
			//	$("input[name='code01']").val("");
			//	$("input[name='code02']").val("");
			//	$("input[name='code01']").attr("disabled", true);
			//	$("input[name='code02']").attr("disabled", true);
			//	document.getElementById("error01").innerHTML = "短信验证码发送失败";
			//	document.getElementById("error02").innerHTML = "请重新打开此窗口";
			//}
		};

		sendApiNoLoad(url, params, cb, 'POST');
	},

	bindEvent : function() {
		$('.transferBack').unbind();
		$('.transferBack').on('click', function() {
					var thisObj = $(this);


					var  canDo = true;

					var nowTime = new Date().getTime();


					/*只有企业回款才要验证输入的手续费*/
					var repayPoundage = $("input[name='repayPoundage']").val().trim();
					if(productPayBack.creditorType == 1){

						/*0-499的验证*/
						//var NumReg = /(^[0-4]\d\d$)|(^\d\d$)|(^\d$)/;
						var NumReg = /^\d+(\.\d{0,2})?$/;
						if(repayPoundage == ''){
							document.getElementById("repayPoundageError").innerHTML = "手续费不能为空！";
							return false;
						}else if(NumReg.test(repayPoundage) == false || repayPoundage > 499 || repayPoundage < 0 ){
							document.getElementById("repayPoundageError").innerHTML = "手续费格式错误，请输入0-499的金额！";
							return false;
						}else {
							document.getElementById("repayPoundageError").innerHTML = "";
						}
					}
					productPayBack.repayPoundage = repayPoundage;


					var code01 = $("input[name='code01']").val().trim();
					var code02 = $("input[name='code02']").val().trim();

					var reg = new RegExp("[0-9]{6}", "");


					if(code01 == ''){
						document.getElementById("error01").innerHTML = "数字验证码不能为空！";
						return false;
					}else if (code01.length != 6 || reg.test(code01) == false ){
						document.getElementById("error01").innerHTML = "验证码格式错误，请输入6位数字验证码！";
						return false;
					}else{
						document.getElementById("error01").innerHTML = '';
					}

					if(code02 == ''){
						document.getElementById("error02").innerHTML = "数字验证码不能为空！";
						return false;
					}else if (code02.length != 6 || reg.test(code02) == false){
						document.getElementById("error02").innerHTML = "验证码格式错误， 请输入6位数字验证码！";
						return false;
					}else{
						document.getElementById("error02").innerHTML = '';
					}

					productPayBack.code01 = code01;

					productPayBack.code02 = code02;

					if(code01 == null || code02 == null){
						alert("短信验证码不存在，请输入短信验证码。")
					}


					$("#productType02_107").attr("disabled",true);

					if(nowTime < productPayBack.payBackTime){
						art.dialog.confirm('当前时间早于该产品的回款时间，是否仍要继续？', function() {
							productPayBack.startPayBack();
						}, function(){
						});
					}else if(nowTime - productPayBack.payBackTime > 16 *  3600 *1000){
						art.dialog.confirm('当前时间晚于该产品的回款时间，是否仍要继续？', function() {
							productPayBack.startPayBack();
						}, function(){
						});
					}else{
						productPayBack.startPayBack();
					}


				}
		);
	},
	init : function() {
		$("#productType02_107").attr("disabled",true);
		$(".repayPoundage").hide(); // 回款手续费默认是隐藏，只有企业才要显示

		productPayBack.showProdOrderInfo();

		jsUtils.controlFunctionBtn();

		productPayBack.bindEvent();

	}
}
$(document).ready(function() {
	productPayBack.init();
});

function submitForm() {
	if (productPayBack.startDiv == 0) {
		comValidform.submitForm();
	} else {
		alert("产品已起标，不能再次起标");
	}
	return returnStatus;
}


